home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / gg243730.zip / MEMLAB1.C next >
Text File  |  1993-03-06  |  5KB  |  126 lines

  1. /**********************************************************/
  2. /**********************************************************/
  3. /***                                                    ***/
  4. /***  Program name: MEMLAB1.EXE                         ***/
  5. /***                                                    ***/
  6. /***  Created     : 7. May 1990                         ***/
  7. /***                                                    ***/
  8. /***  Author      : Bo Falkenberg                       ***/
  9. /***                                                    ***/
  10. /***  Revised     : February, 1992 by Darryl Frost      ***/
  11. /***                                                    ***/
  12. /***  Purpose     : To demonstrate the use of the new   ***/
  13. /***                DosAllocMem API, and the handling   ***/
  14. /***                of General Protection Exceptions.   ***/
  15. /***                                                    ***/
  16. /***  Compile     : icc /W2 memlab1.c;                  ***/
  17. /***                                                    ***/
  18. /***  Execute     : memlab1 (No command line parameters)***/
  19. /***                                                    ***/
  20. /***  Input param : 1. Memory to allocate               ***/
  21. /***                2. Memory to use for read/write     ***/
  22. /***                                                    ***/
  23. /**********************************************************/
  24. /**********************************************************/
  25.  
  26.  
  27.  
  28. /**********************************************************/
  29. /***  DEFINES                                           ***/
  30. /**********************************************************/
  31. #define INCL_DOSMEMMGR
  32.  
  33. /**********************************************************/
  34. /***  INCLUDE                                           ***/
  35. /**********************************************************/
  36. #include <os2.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <signal.h>
  40.  
  41. /***      GLOBAL VARIABLES                              ***/
  42. ULONG     ulLoop;     /* loop variable                    */
  43.  
  44. /***      FUNCTION PROTOTYPES                           ***/
  45. void main(int argc, char *argv[], char *envp[]);
  46. void traphandler(int sig);
  47. void normalexit(void);
  48.  
  49. /***  MAIN PROGRAM                                      ***/
  50. void main(int argc, char *argv[], char *envp[])
  51. {                        /*******************************************/
  52.    PULONG    pulBlock;   /* pointer to the starting memory location */
  53.    ULONG     ulErr;      /* error variable                          */
  54.    ULONG     ulAmount;   /* amount of memory to be allocated        */
  55.                          /*             (long integers)             */
  56.    ULONG     ulBytes;    /* amount of memory to be allocated (bytes)*/
  57.    ULONG     ulUse;      /* amount of memory to be used             */
  58.                          /*             (long integers)             */
  59.    BOOL      OK = TRUE;  /* memory check indicator                  */
  60.                          /*******************************************/
  61.    setbuf(stdout, NULL);
  62. /* Register an exception handler for memory exception     */
  63.    if (signal(SIGSEGV, traphandler) != SIG_ERR)
  64.       printf("\nSignal Handler registered for memory exceptions\n");
  65. /* Register an exit routine for normal exits */
  66.    if (atexit(normalexit) == 0)
  67.       printf("\nExit handler for normal termination registered\n");
  68. /* Read parameters: 1. Memory to allocate    */
  69. /*                  2. Memory to use         */
  70. /* Both parameters as number of long integers*/
  71.    printf("\nFor how many long integers should memory be allocated : ");
  72.    scanf("%u", &ulAmount);
  73. /* Determine number of bytes to allocate     */
  74.    ulBytes = ulAmount * sizeof(ULONG);
  75.    printf("\nHow long integers should be written into this memory : ");
  76.    scanf("%u", &ulUse);
  77. /* Allocate the memory                       */
  78.    ulErr = DosAllocMem ( (PPVOID)&pulBlock, ulBytes,
  79.                          PAG_COMMIT | PAG_READ | PAG_WRITE);
  80.    if (!ulErr)
  81.    {
  82. /* Insert values into ulUse memory           */
  83.       printf("\nInserting integers into memory\n");
  84.       for (ulLoop = 0; ulLoop < ulUse; ulLoop++)
  85.       {
  86.          *(pulBlock + ulLoop) = '\xAB';
  87.       } /* endfor */
  88. /* Read the memory to check that it is OK    */
  89.       for (ulLoop = 0; ulLoop < ulUse; ulLoop++)
  90.       {
  91.          if (*(pulBlock + ulLoop) != '\xAB')
  92.          {
  93.             printf("\nError in byte %u\n", ulLoop);
  94.             OK = FALSE;
  95.          } /* endif */
  96.       } /* endfor */
  97.  
  98.       if (OK)
  99.       {
  100.          printf("\nAll memory checked out OK\n");
  101.       } /* endif */
  102.  
  103. /* Free the memory                           */
  104.       ulErr = DosFreeMem (pulBlock);
  105.       if (ulErr != 0)
  106.       {
  107.         printf("\nError in freeing : code %u\n", ulErr);
  108.       } /* endif */
  109.    } else
  110.    {
  111.      printf("\nError in allocation : code %u\n", ulErr);
  112.    } /* endif */
  113.  
  114. }
  115. /* ABNORMAL TERMINATION HANDLER              */
  116. void traphandler (int sig)
  117. {
  118.      printf("\nA General Protection Exception was detecting writing to"
  119.             " position %u\n", ulLoop+1);
  120. }
  121. /* NORMAL TERMINATION ROUTINE                */
  122. void normalexit(void)
  123. {
  124.      printf("\n%u integers successfully inserted into memory\n", ulLoop);
  125. }
  126.